package com.bike.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhAgent;
import com.bike.entity.HhBusiness;
import com.bike.entity.HhBusinessParent;
import com.bike.entity.HhBusinessRecord;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhPayRecord;
import com.bike.entity.HhWithdraw;
import com.bike.mapper.HhAgentMapper;
import com.bike.mapper.HhBusinessMapper;
import com.bike.mapper.HhBusinessParentMapper;
import com.bike.mapper.HhBusinessUserMapper;
import com.bike.mapper.HhPayRecordMapper;
import com.bike.mapper.HhWithdrawMapper;
import com.bike.util.Constant;
import com.bike.util.Md5Util;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhBusinessUserServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhBusinessUserMapper hhBusinessUserMapper;
	@Autowired
	HhBusinessMapper hhBusinessMapper;
	@Autowired
	HhBusinessParentMapper hhBusinessParentMapper;
	@Autowired
	HhWithdrawMapper hhWithdrawMapper;
	@Autowired
	HhAgentMapper hhAgentMapper;
	@Autowired
	HhPayRecordMapper hhPayRecordMapper;
	@Autowired
	HhAgentServiceImpl hhAgentServiceImpl;
	@Autowired
	HhPayRecordServiceImpl hhPayRecordServiceImpl;
	/**
	* 保存
	*/
	public int saveOrUpdate(PageData pd, HhBusinessUser record) {
		long businessId=pd.getLong("businessId");
		long parentId=pd.getLong("parentId");
		if(businessId>0){
			HhBusiness business= hhBusinessMapper.selectByPrimaryKey(businessId);
			record.setBusinessName(business.getName());
		}
		if(parentId>0){
			HhBusinessParent business= hhBusinessParentMapper.selectByPrimaryKey(parentId);
			record.setBusinessName(business.getName());
			record.setBusinessId(parentId);
			record.setType(1);
		}
		
		
		if(!StringUtils.isEmpty(record.getPwd())){
			record.setPwd(Md5Util.MD5(record.getPwd()));
		}
		if(pd.getLong("businessUserId")>0){
			 //编辑
			return hhBusinessUserMapper.updateByPrimaryKeySelective(record);
		}else{
			if(StringUtils.isEmpty(record.getPhone())){
				return 0;
			}
			HhBusinessUser businessUser= hhBusinessUserMapper.selectByPhone(record.getPhone());
			if(businessUser!=null){
				if(parentId>0&&businessUser.getType()==1){
					return 0;
				}
				if(businessId>0&&businessUser.getType()==0){
					return 0;
				}
			}
			record.setAddTime(TimeManager.currentTime());
			return hhBusinessUserMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhBusinessUserById(long businessUserId) {
		 HhBusinessUser record=new HhBusinessUser();
		 record.setStatus(1);
		 record.setBusinessUserId(businessUserId);
		return hhBusinessUserMapper.updateByPrimaryKeySelective(record);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhBusinessUserList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		if(pd.getLong("parentId")>0){
			pd.put("businessId", pd.getLong("parentId"));
			pd.put("type", 1);
		}else{
			pd.put("type", 0);
		}
		List<HhBusinessUser> records= hhBusinessUserMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhBusinessUser>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhBusinessUser> listAll(PageData pd) {
		return  hhBusinessUserMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhBusinessUser selectByHhBusinessUserId(Long businessUserId) {
		return hhBusinessUserMapper.selectByPrimaryKey(businessUserId);
	}

	public HhBusinessUser selectByPhone(String phone) {
		return hhBusinessUserMapper.selectByPhone(phone);
	}

	public int updateUser(PageData pd, HhBusinessUser user) {
		return hhBusinessUserMapper.updateByPrimaryKeySelective(user);
	}

	public int updateClose(HhBusiness business) {
		return hhBusinessMapper.updateByPrimaryKeySelective(business);
	}

	public int updateBusinessWithdraw(HhBusinessUser businessUser) {
		 HhBusiness business=hhBusinessMapper.selectByPrimaryKey(businessUser.getBusinessId());
			 BigDecimal serviceFee=null;
			BigDecimal businessUserBalance=businessUser.getBalance();
			BigDecimal trueAccount=businessUser.getBalance();
			if(business.getPayWithdraw()==0){
				serviceFee=businessUserBalance.multiply(new BigDecimal(Constant.WXwithdrawFee)).setScale(2, RoundingMode.HALF_UP);
				if(serviceFee.doubleValue()==0){
					serviceFee=new BigDecimal("0.01");
				}
				trueAccount=businessUserBalance.subtract(serviceFee).subtract(new BigDecimal("2"));
			}
			if(trueAccount.doubleValue()<0){
				return 0;
			}
			//余额清零
			businessUser.setBalance(new BigDecimal("0"));
			
			HhBusinessUser businessUser2=new HhBusinessUser();
			businessUser2.setBusinessUserId(businessUser.getBusinessUserId());
			hhBusinessUserMapper.updateBalance(businessUser2);
			
			int time=TimeManager.currentTime();
			//增加提现记录
			HhWithdraw hhWithdraw=new HhWithdraw();
			hhWithdraw.setAddTime(time);
			hhWithdraw.setAmount(trueAccount);
			hhWithdraw.setBank(businessUser.getBank());
			hhWithdraw.setBankBranch(businessUser.getBankBranch());
			hhWithdraw.setBankCode(businessUser.getBankCode());
			hhWithdraw.setBankUser(businessUser.getBankName());
			hhWithdraw.setBusinessId(businessUser.getBusinessUserId());
			hhWithdraw.setBusinessName(businessUser.getName());
			hhWithdraw.setUserBusinessName(businessUser.getName());
			hhWithdraw.setServiceAmount(serviceFee);
			hhWithdraw.setPoundage(new BigDecimal("2"));
			hhWithdraw.setWithdrawAmount(businessUserBalance);
			hhWithdraw.setType(1);
			hhWithdrawMapper.insertSelective(hhWithdraw);
			
			HhAgent agent= hhAgentServiceImpl.insertOrSelectByPrimaryKey();
			//提现金额
			 //交易记录
			 HhPayRecord payRecord=new HhPayRecord();
			 payRecord.setAddTime(TimeManager.currentTime());
			 
			 payRecord.setMoney(trueAccount);
			 payRecord.setWithdrawId(hhWithdraw.getWithdrawId());
			 payRecord.setUserId(businessUser2.getBusinessUserId());
			 payRecord.setUserName(businessUser2.getName());
			 payRecord.setToUserId(agent.getAgentId());
			 payRecord.setToUserName(agent.getName());
			 payRecord.setTransType(Constant.withdraw);
			 payRecord.setStatus(0);
			 payRecord.setType(Constant.employPayRecordType);
			 hhPayRecordMapper.insertSelective(payRecord);
			 
			 hhPayRecordServiceImpl.insertAgentPayRecord(agent, trueAccount, Constant.platform, "用户提现金额",true);
			//提现手续费
			 if(serviceFee!=null){
				 HhPayRecord payRecordSer=new HhPayRecord();
				 payRecordSer.setAddTime(TimeManager.currentTime());
				 payRecordSer.setMoney(serviceFee);
				 payRecordSer.setWithdrawId(hhWithdraw.getWithdrawId());
				 payRecordSer.setUserId(businessUser2.getBusinessUserId());
				 payRecordSer.setUserName(businessUser2.getName());
				 payRecordSer.setToUserId(agent.getAgentId());
				 payRecordSer.setToUserName(agent.getName());
				 payRecordSer.setTransType(Constant.withdrawService);
				 payRecordSer.setType(1);
				 hhPayRecordMapper.insertSelective(payRecordSer);
				 hhPayRecordServiceImpl.insertAgentPayRecord(agent, trueAccount, Constant.platform, "用户提现手续费",true);
			 }
			//银联转账手续费
			 HhPayRecord payRecordSer=new HhPayRecord();
			 payRecordSer.setAddTime(TimeManager.currentTime());
			 
			 payRecordSer.setMoney(new BigDecimal("2"));
			 payRecordSer.setWithdrawId(hhWithdraw.getWithdrawId());
			 payRecordSer.setUserId(businessUser2.getBusinessUserId());
			 payRecordSer.setUserName(businessUser2.getName());
			 payRecordSer.setToUserId(agent.getAgentId());
			 payRecordSer.setToUserName(agent.getName());
			 payRecordSer.setType(1);
			 payRecordSer.setTransType(Constant.withdrawBankFeeType);
			 hhPayRecordMapper.insertSelective(payRecordSer);
			 
			//更新系统平台代理商余额  加入，提现完成 在减掉
			agent.setBalance(agent.getBalance().add(businessUserBalance ));
			hhPayRecordServiceImpl.insertAgentPayRecord(agent, businessUserBalance, Constant.platform, "用户提现银行转账手续费",true);
			 return hhAgentMapper.updateByPrimaryKeySelective(agent);
	}

}

